<template>
  <div class='container-subjects'>
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <!-- 表单 -->
        <el-col :span='18'>
          <el-form label-width='80px' inline size='small'>
            <el-form-item label='学科管理'>
              <el-input v-model="reqParams.subjectName" placeholder="搜索关键字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 按钮 -->
        <el-button
        style="float:right" type="success"
        icon="el-icon-edit"
        @click='amend()'
        size="small">新增学科</el-button>
        <el-col :span="6"></el-col>
        <!-- 提示 -->
      </el-row>
       <el-alert
        :title="`数据一共${total}条`"
        type="info"
        :closable="false"
        style='margin-bottom:15px'
        show-icon>
      </el-alert>
      <!-- 总计 -->
      <!-- 表单 -->
      <el-table :data="subjects">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="学科名称" prop="subjectName" ></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            {{scope.row.addDate | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">
          {{scope.row.isFrontDisplay ===1 ?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
        <el-table-column label="标签" prop="tags"></el-table-column>
        <el-table-column label="题目数量" prop="totals"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button type="text" @click="toSub(scope.row)">学科分类</el-button>
            <el-button type="text" @click="toTag(scope.row)">学科标签</el-button>
            <el-button type="text" @click="amend(scope.row)">修改</el-button>
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 sizes指页面码数 jumper页面跳转  -->
      <el-pagination
        class="pager"
        background
        layout="prev, pager, next, sizes,jumper"
        :total="total"
        :page-size="reqParams.pagesize"
        :current-page="reqParams.page"
        @current-change='changePage'
        @size-change='changeSize'
        :page-sizes="[5,10,20,50]"
        >
      </el-pagination>
    </el-card>
<!-- 修改学科 -->
<subjects-add ref="subjectsadd"
 :data="currSubject"
 @updataList='updataList'></subjects-add>
  </div>
</template>
<script>
// 学科列表按需导入
import { list, remove } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'SubjectsList',
  components: { SubjectsAdd },
  data () {
    return {
      reqParams: {
        // 学科名字
        subjectName: null,
        // 页码
        page: 1,
        pagesize: 10
      },
      // 表单
      subjects: [],
      // 总条数,
      total: 0,
      currSubject: {}
    }
  },
  created () {
    this.getSubejcts()
  },
  methods: {
    // 去目录 学科目录分类
    toSub (row) {
      const { id, subjectName: name } = row
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    // 去标签
    toTag (row) {
      const { id, subjectName: name } = row
      this.$router.push({ path: 'tags', query: { id, name } })
    },
    // 获取学科列表
    async getSubejcts () {
      const { data } = await list(this.reqParams)
      // const res = await list(this.reqParams)
      // console.log(res)
      this.subjects = data.items
      // 总条数
      this.total = data.counts
    },
    // 分页
    changePage (newPage) {
      this.reqParams.page = newPage
      this.getSubejcts()
    },
    // 改变尺码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.getSubejcts()
    },
    // 搜索 因为获取之前要把页码改成第一页，在重新获取数据
    search () {
      this.reqParams.pagesize = 5
      this.getSubejcts()
    },
    // 清除
    clear () {
      this.reqParams = {
        subjectName: null,
        // 页码
        page: 1,
        pagesize: 5
      }
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getSubejcts()
      }).catch(e => {})
    },
    // 修改
    amend (subject) {
      // console.log(111)
      this.currSubject = subject || {}
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    // 修改更新列表
    updataList () {
      if (!this.currSubject.id) {
        this.reqParams.page = 1
      }
      this.getSubejcts()
    }
  }
}
</script>

<style scoped lang='scss'>
.container-subjects{
.pager{
  margin-top: 15px;
  //浮动
  margin-bottom: 15px;
  float: right;
}
.el-link:hover{
  text-decoration: none;
}
}
</style>
